#include <algorithm>
#include <boost/bind.hpp>
#include "adapter_car.h"
#include "shader_update_messages.h"
#include <automessage.h>
#include <pantheios/pantheios.hpp>
#include <pantheios/inserters.hpp>
#include "shader/car_seter.h"
#include "shader_mgr.h"
#include "racing_game_logic.h"
#include "iltclient.h"
#include "clientinterfaces.h"
extern IDirect3DDevice9* g_Direct3DDevice9 ;
adapter_car::adapter_car(HOBJECT obj,
                         racing_game_logic* logic,
                         const std::vector<int>& ids,
                         const std::string& cubename) :
    adapter_object(obj),
    m_shader_ids(ids),
    m_racing_logic(logic) {
    init_messages() ;
    //D3DXCreateCubeTextureFromFile(g_Direct3DDevice9, "LobbyCube.dds", &m_cube) ;

    for(size_t i = 0 ; i < m_shader_ids.size() ; ++i) {
        shader_manager* mgr = logic->game_shader_manager() ;
        car_seter* m_seter = new car_seter(mgr, cubename) ;
        m_seter->bind_obj(obj) ;
        mgr->bind_seters(static_cast<ShaderIDS>(m_shader_ids[i]), m_seter) ;
        m_seters.push_back(m_seter) ;
        
    }
	change_color=false;
}

void adapter_car::choose_light_to_use() {
    
}

void adapter_car::do_update(float time_inteval) {
    darker_object darker = m_racing_logic->get_darker_factor(get_object()) ;
    choose_light_to_use() ;
    LTVector pos = boost::get<DOE_POSITION>(darker) ;
    float radius = boost::get<DOE_RADIUS>(darker) ;
    float factor = boost::get<DOE_INFULENCE>(darker) ;
    std::for_each(m_seters.begin(), m_seters.end(),
                  boost::bind(&car_seter::set_light_pos,
                              _1,pos,factor, radius)) ;




	/*
	if (m_racing_logic->get_car_speed_KMPH()>180)
	{
		if (!change_color)
		{	
			std::vector<car_seter*>::iterator it;
			for (it=m_seters.begin();it!=m_seters.end();++it)
			{
				(*it)->set_tail_color(0.0f,0.3f,0.2f);
			}
			change_color=true;
			
		}
				
	}
	else
	{
		if (change_color)
		{
			change_color=false;
			std::vector<car_seter*>::iterator it;
			for (it=m_seters.begin();it!=m_seters.end();++it)
			{
				(*it)->set_tail_color();
			}
		}
	

	}
	*/


}

void adapter_car::init_messages() {
    m_shader_updater.add(static_cast<int>(SUME_UPDATE_CAR),
                         this, &adapter_car::handle_update_car_message) ;
}

void adapter_car::handle_update_car_message(ILTMessage_Read* pMsg) {
    pantheios::log_DEBUG("handle shader update car message") ;
    LTVector pos(0,0,0) ;
       
}
